<?php
namespace app\admin;

use app\model\AdminMenuModel;
use app\model\AdminModel;
use app\model\AdminRolesModel;

//鉴权类
class Auth
{
    /**
     * 验证是否有权限
     */
    static function canAccess($admin_id,$controller,$action,$app){

        //用户角色
        $menu_ids = AdminRolesModel::getAdminMenu($admin_id);

        if(empty($menu_ids)) return false;

        //判断超级管理员
        if(in_array('*',$menu_ids)){
            return true;
        }

        //格式化controller
        $controller = str_replace('Controller','',end(explode("\\",$controller)));

        //判断权限是否添加到数据库
        $menu_id = AdminMenuModel::where([
            ['type','=','2'],
            ['path','=',strtolower($app.'/'.$controller.'/'.$app)]
        ])->value("id");

        //未添加的权限，默认放行
        if(!$menu_id) return true;

        //验证权限
        if(in_array($menu_id,$menu_ids)){
            return true;
        }

        return false;
    }


}